package com.yonyougov.devops.cruxcisupport.versioninfo;

import com.yonyougov.devops.cruxcisupport.committest.CommitTestService;
import com.yonyougov.devops.cruxcisupport.committest.dto.QueryDto;
import com.yonyougov.devops.cruxcisupport.committest.vo.QueryCommitTestVo;
import com.yonyougov.devops.cruxcisupport.roleapp.RoleAppService;
import com.yonyougov.devops.cruxcisupport.versioninfo.dto.VersionInfoPageDto;
import com.yonyougov.devops.cruxcisupport.versioninfo.vo.VersionInfoPageVo;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * t_version_info服务实现
 */
@Service
public class VersionInfoServiceImpl extends BaseServiceImpl<VersionInfoEntity, BigInteger> implements VersionInfoService {
    private final VersionInfoDao versionInfoDao;
    @Autowired
    private CommitTestService commitTestService;
    @Autowired
    private RoleAppService roleAppService;

    public VersionInfoServiceImpl(VersionInfoDao versionInfoDao) {
        this.versionInfoDao = versionInfoDao;
    }

    @Override
    public BaseDao<VersionInfoEntity, BigInteger> getDao() {
        return versionInfoDao;
    }


    /**
     * 查询版本信息(附带返回提测单信息)
     * 条件: 应用名称, 版本名称
     *
     * @param versionInfoPageDto [com.yonyougov.devops.cruxcisupport.versioninfo.dto.VersionInfoPageDto]
     * @return [com.yonyougov.devops.fwc.core.entity.Pagination]
     */
    @Override
    public Pagination<VersionInfoPageVo> findPageBy(VersionInfoPageDto versionInfoPageDto) throws Exception {
        // 内置 "当前用户下的应用" 作为 "查询条件"
        versionInfoPageDto.setAppIds(roleAppService.getCurrentUserAppIds());

        // 版本信息数据
        List<VersionInfoPageVo> versionInfoPageVoList = versionInfoDao.findPageBy(versionInfoPageDto);
        int totalCount = versionInfoDao.countBy(versionInfoPageDto);

        // 附带返回提测单内容
        for (VersionInfoPageVo versionInfoPageVo : versionInfoPageVoList) {
            if (null != versionInfoPageVo.getCommitTestid()) {
                QueryCommitTestVo queryCommitTestVo = commitTestService.query(QueryDto.builder().commitTestId(versionInfoPageVo.getCommitTestid()).build());
                if (Objects.isNull(queryCommitTestVo)) {
                    versionInfoPageVo.setCommitTestid(null);
                    continue;
                }
                BeanUtils.copyProperties(queryCommitTestVo, versionInfoPageVo, new String[]{"id", "createTime"});
                versionInfoPageVo.setCommitTestCreateTime(queryCommitTestVo.getCreateTime());
            }
        }

        return new Pagination<>(versionInfoPageDto.getMax(), versionInfoPageDto.getPageNum(), totalCount, versionInfoPageVoList);
    }

    @Override
    public Pagination<VersionInfoPageVo> findNotRelation() {
        List<VersionInfoPageVo> versionInfoPageVoList = versionInfoDao.findNotRelation();
        return new Pagination<>(99999, 1, versionInfoPageVoList.size(), versionInfoPageVoList);

    }
    //<!--用户自定义代码结束-->
}
